<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Dynamic Thread Pool 「动态线程池」</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f7f7f7;
        }
        .container {
            max-width: 100%;
            margin: auto;
            background: #fff;
            padding: 20px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        table, th, td {
            border: 1px solid #ddd;
        }
        th, td {
            padding: 10px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
        tr:nth-child(even) {
            background-color: #f9f9f9;
        }
        .loader {
            display: none;
            text-align: center;
        }
        .modal {
            display: none;
            position: fixed;
            z-index: 1;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            overflow: auto;
            background-color: rgba(0,0,0,0.5);
            padding-top: 100px;
        }
        .modal-content {
            background-color: #fefefe;
            margin: auto;
            padding: 20px;
            border: 1px solid #888;
            width: 80%;
            max-width: 500px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.2);
            border-radius: 10px;
        }
        .close {
            color: #aaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
        }
        .close:hover {
            color: black;
            cursor: pointer;
        }
        .modal input[type="text"],
        .modal input[type="number"] {
            width: calc(100% - 10px);
            padding: 8px;
            margin-bottom: 15px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            margin: 10px 0;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>Dynamic Thread Pool —— 动态线程池</h1>
    <div class="loader">Loading...</div>
    <button id="autoRefreshBtn">自动刷新</button>
    <button id="stopAutoRefreshBtn" style="display:none;">停止刷新</button>
    <table id="threadPoolList">
        <thead>
        <tr>
            <th>应用名称</th>
            <th>线程池名称</th>
            <th>核心线程池数</th>
            <th>最大线程数</th>
            <th>当前活跃线程数</th>
            <th>当前池中线程数</th>
            <th>队列类型</th>
            <th>当前队列任务数</th>
            <th>队列剩余容量数</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody></tbody>
    </table>
</div>

<!-- 模态框 -->
<div id="myModal" class="modal">
    <div class="modal-content">
        <span class="close">&times;</span>
        <h2>线程池配置</h2>
        <form id="configForm">
            <label>应用名称:</label>
            <input type="text" id="appName" readonly>
            <label>线程池名称:</label>
            <input type="text" id="threadPoolName" readonly>
            <label>核心线程池数:</label>
            <input type="number" id="corePoolSize">
            <label>最大线程数:</label>
            <input type="number" id="maximumPoolSize">
            <button type="button" id="updateBtn">确认修改</button>
        </form>
    </div>
</div>

<script>
    (function () {
        const loader = document.querySelector('.loader');
        const tbody = document.querySelector('#threadPoolList tbody');
        let autoRefreshInterval = null;

        // 显示加载
        function showLoader() { loader.style.display = 'block'; }
        function hideLoader() { loader.style.display = 'none'; }

        // 获取线程池列表
        window.fetchThreadPoolList = function () {
            showLoader();
            fetch('http://localhost:8080/api/v1/dynamic/thread/pool/query_thread_pool_list')
                .then(res => res.json())
                .then(response => {
                    if (response.code === 0 && Array.isArray(response.data)) {
                        tbody.innerHTML = response.data.map(item =>
                            `<tr>
                            <td>${item.appName}</td>
                            <td>${item.threadPoolName}</td>
                            <td>${item.corePoolSize}</td>
                            <td>${item.maximumPoolSize}</td>
                            <td>${item.activeCount}</td>
                            <td>${item.poolSize}</td>
                            <td>${item.queueType}</td>
                            <td>${item.queueSize}</td>
                            <td>${item.remainingCapacity}</td>
                            <td><button onclick="openModal('${item.appName}', '${item.threadPoolName}')">修改</button></td>
                        </tr>`).join('');
                    } else {
                        console.error('数据格式错误', response);
                    }
                })
                .catch(err => console.error('请求失败', err))
                .finally(hideLoader);
        };

        // 打开模态框
        window.openModal = function (appName, threadPoolName) {
            fetch(`http://localhost:8080/api/v1/dynamic/thread/pool/query_thread_pool_config?appName=${appName}&threadPoolName=${threadPoolName}`)
                .then(res => res.json())
                .then(response => {
                    if (response.code === 0 && response.data) {
                        document.getElementById('appName').value = response.data.appName;
                        document.getElementById('threadPoolName').value = response.data.threadPoolName;
                        document.getElementById('corePoolSize').value = response.data.corePoolSize;
                        document.getElementById('maximumPoolSize').value = response.data.maximumPoolSize;
                        document.getElementById('myModal').style.display = "block";
                    } else {
                        console.error('获取配置失败', response);
                    }
                })
                .catch(err => console.error('请求失败', err));
        };

        // 更新配置
        document.getElementById('updateBtn').addEventListener('click', function () {
            const appName = document.getElementById('appName').value;
            const threadPoolName = document.getElementById('threadPoolName').value;
            const corePoolSize = parseInt(document.getElementById('corePoolSize').value, 10);
            const maximumPoolSize = parseInt(document.getElementById('maximumPoolSize').value, 10);

            fetch('http://localhost:8080/api/v1/dynamic/thread/pool/update_thread_pool_config', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ appName, threadPoolName, corePoolSize, maximumPoolSize })
            })
                .then(res => res.json())
                .then(response => {
                    if (response.code === 0) {
                        alert('更新成功');
                        document.getElementById('myModal').style.display = "none";
                        fetchThreadPoolList();
                    } else {
                        alert('更新失败: ' + response.info);
                    }
                })
                .catch(err => alert('请求失败: ' + err));
        });

        // 自动刷新按钮
        document.getElementById('autoRefreshBtn').addEventListener('click', function () {
            if (!autoRefreshInterval) {
                autoRefreshInterval = setInterval(fetchThreadPoolList, 3000);
                this.style.display = 'none';
                document.getElementById('stopAutoRefreshBtn').style.display = 'inline';
            }
        });
        document.getElementById('stopAutoRefreshBtn').addEventListener('click', function () {
            clearInterval(autoRefreshInterval);
            autoRefreshInterval = null;
            this.style.display = 'none';
            document.getElementById('autoRefreshBtn').style.display = 'inline';
        });

        // 模态框关闭
        document.getElementsByClassName("close")[0].onclick = () => document.getElementById('myModal').style.display = "none";
        window.onclick = e => { if (e.target == document.getElementById('myModal')) document.getElementById('myModal').style.display = "none"; };

        // 初始加载
        fetchThreadPoolList();
    })();
</script>
</body>
</html>
